package com.dmwork.service.interaction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.common.base.result.ResultCodeEnum;
import com.dmwork.service.base.exception.DmworkException;
import com.dmwork.service.interaction.entity.FeedComment;
import com.dmwork.service.interaction.entity.WorkComment;
import com.dmwork.service.interaction.entity.vo.CommentMessageVo;
import com.dmwork.service.interaction.entity.vo.FeedCommentMessageVo;
import com.dmwork.service.interaction.entity.vo.WorkCommentMessageVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.mapper.FeedCommentMapper;
import com.dmwork.service.interaction.service.FeedCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 动态评论表 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-05-07
 */
@Service
public class FeedCommentServiceImpl extends ServiceImpl<FeedCommentMapper, FeedComment> implements FeedCommentService {

    @Autowired
    private CustomerFeignService customerFeignService;

    @Override
    public void decreaseLikeCount(String commentId) {
        baseMapper.decreaseLikeCount(commentId);
    }

    @Override
    public void increaseLikeCount(String commentId) {
        baseMapper.increaseLikeCount(commentId);
    }

    @Override
    public Page<FeedComment> getCommentPage(String feedId, Integer sortType, Integer page, Integer limit) {
        QueryWrapper<FeedComment> queryWrapper = new QueryWrapper<>();
        Page<FeedComment> selectPage = new Page<>(page, limit);
        queryWrapper.eq("feed_id", feedId).isNull("parent_comment_id");
        if (sortType == 0) {
            queryWrapper.orderByDesc("like_count");
        } else if (sortType == 1) {
            queryWrapper.orderByDesc("gmt_create");
        }
        return baseMapper.selectPage(selectPage, queryWrapper);
    }

    @Override
    public Page<FeedComment> getReplyPage(String commentId, Integer sortType, Integer page, Integer limit) {
        QueryWrapper<FeedComment> queryWrapper = new QueryWrapper<>();
        Page<FeedComment> feedCommentPage = new Page<>(page, limit);
        queryWrapper.eq("parent_comment_id", commentId);
        if (sortType == 0) {
            queryWrapper.orderByDesc("like_count");
        } else if (sortType == 1) {
            queryWrapper.orderByDesc("gmt_create");
        }
        return baseMapper.selectPage(feedCommentPage, queryWrapper);
    }

    @Override
    public List<FeedCommentMessageVo> getReplyMessageList(List<String> commentIdList) {
        List<FeedCommentMessageVo> feedCommentMessageVoList = new ArrayList<>();
        //先查出所有回复的数据
        List<FeedComment> feedCommentList = baseMapper.selectList(new QueryWrapper<FeedComment>().in("id", commentIdList).orderByDesc("gmt_create"));
        //遍历获取这些回复所回复的评论id，并查出这些被回复的评论
        List<String> repliedCommentIdList=new ArrayList<>();
        for (FeedComment feedComment : feedCommentList) {
            repliedCommentIdList.add(feedComment.getRepliedCommentId());
        }
        List<FeedComment> repliedCommentList = baseMapper.selectBatchIds(repliedCommentIdList);
        //对回复消息的数据进行组装
        for (FeedComment feedComment : feedCommentList) {
            FeedCommentMessageVo feedCommentMessageVo = new FeedCommentMessageVo();
            feedCommentMessageVo.setCustomerId(feedComment.getCustomerId());
            feedCommentMessageVo.setCommentId(feedComment.getId());
            feedCommentMessageVo.setComment(feedComment.getContent());
            feedCommentMessageVo.setParentCommentId(feedComment.getParentCommentId());
            feedCommentMessageVo.setFeedId(feedComment.getFeedId());
            feedCommentMessageVo.setGmtCreate(feedComment.getGmtCreate());
            for (FeedComment repliedComment : repliedCommentList) { //对被回复的评论进行遍历，找到当前回复的评论并进行数据组装
                if (repliedComment.getId().equals(feedComment.getId())) {
                    feedCommentMessageVo.setRepliedCommentId(repliedComment.getId());
                    feedCommentMessageVo.setRepliedComment(repliedComment.getContent());
                    break;
                }
            }
            feedCommentMessageVoList.add(feedCommentMessageVo);
        }
        return feedCommentMessageVoList;
    }

    @Override
    public List<CommentMessageVo> getCommentMessageList(List<String> feedCommentIdList) {
        //查询评论表中数据
        List<FeedComment> feedCommentList = baseMapper.selectBatchIds(feedCommentIdList);
        //查询所有评论用户的用户名和头像
        List<String> customerIdList=new ArrayList<>(); //用户id列表
        List<String> repliedCommentIdList=new ArrayList<>(); //被回复的评论id列表
        for (FeedComment feedComment : feedCommentList) {
            customerIdList.add(feedComment.getCustomerId());
            repliedCommentIdList.add(feedComment.getRepliedCommentId());
        }
        R customerBriefInfoListResult = customerFeignService.getBriefInfoList(customerIdList);
        if (!customerBriefInfoListResult.getSuccess()) {
            throw new DmworkException(ResultCodeEnum.SELECT_CUSTOMER_INFO_ERROR);
        }
        //查询被回复的评论数据
        List<FeedComment> repliedWorkCommentList = baseMapper.selectBatchIds(repliedCommentIdList);
        Map<String, List<String>> customerInfoMap = (Map<String, List<String>>) customerBriefInfoListResult.getData().get("result");
        //数据组装
        List<CommentMessageVo> commentMessageVoList=new ArrayList<>();
        for (FeedComment feedComment : feedCommentList) {
            CommentMessageVo commentMessageVo = new CommentMessageVo();
            BeanUtils.copyProperties(feedComment, commentMessageVo);
            commentMessageVo.setCommentId(feedComment.getId());
            commentMessageVo.setComment(feedComment.getContent());
            commentMessageVo.setType(1);
            commentMessageVo.setCustomerName(customerInfoMap.get(feedComment.getCustomerId()).get(0));
            commentMessageVo.setCustomerAvatar(customerInfoMap.get(feedComment.getCustomerId()).get(1));
            for (FeedComment repliedComment : repliedWorkCommentList) {
                if (repliedComment.getId().equals(feedComment.getRepliedCommentId())) {
                    commentMessageVo.setRepliedComment(repliedComment.getContent());
                    break;
                }
            }
            commentMessageVoList.add(commentMessageVo);
        }
        return commentMessageVoList;

    }
}
